package com.tfdy.util.shp;

import com.tfdy.util.geo.CoordinateTransformUtil;
import com.tfdy.vo.YushuiVo;
import org.geotools.data.FeatureWriter;
import org.geotools.data.FileDataStoreFactorySpi;
import org.geotools.data.Query;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.shapefile.files.ShpFiles;
import org.geotools.data.shapefile.shp.ShapefileReader;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.filter.text.cql2.CQL;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.*;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.geometry.BoundingBox;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;


/**
 * Created by zlx on 2017/7/31.
 */
public class CovertShpCoords
{

    //读取shp文件 点
    public static List<Map<Integer,String>> readShpPoint(String buffer,String fileType,List<Point> pointList, String pointFilePath, String annoFilePath) {
        List<Map<Integer,String>> yushuiVoList = new ArrayList<>();
        try {
            ShapefileDataStore annoDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(annoFilePath).toURI().toURL());
            annoDS.setCharset(  Charset.forName("UTF-8"));
            SimpleFeatureSource annoSFS = annoDS.getFeatureSource(annoDS.getTypeNames()[0]);
            String annoFilterStr = "";
            if(fileType.equals("雨水")){
                annoFilterStr = "Layer='YST'";
            }else{
                annoFilterStr = "Layer='WST'";
            }

            Filter annoFilter = CQL.toFilter(annoFilterStr);
            Query annoQuery = new Query();
            annoQuery.setFilter(annoFilter);
            SimpleFeatureCollection annoSFC = annoSFS.getFeatures(annoFilter);
            Object[] annoObjects = annoSFC.toArray();
            System.out.println("标注图层总记录数："+ annoSFC.size());
            FeatureIterator<SimpleFeature> annoFI = annoSFC.features();
            List<SimpleFeature> annoList = new ArrayList<>();
            while (annoFI.hasNext()) {
                SimpleFeature simpleFeature = annoFI.next();
                annoList.add(simpleFeature);
            }
            //源shape文件
            ShapefileDataStore pointDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(pointFilePath).toURI().toURL());
            pointDS.setCharset(  Charset.forName("UTF-8"));
            SimpleFeatureSource pointSFS = pointDS.getFeatureSource(pointDS.getTypeNames()[0]);
            String pointFilterStr = "";
            if(fileType.equals("雨水")){
                pointFilterStr = "Layer='YSP'";
            }else{
                pointFilterStr = "Layer='WSP'";
            }
            Filter pointFilter = CQL.toFilter(pointFilterStr);
            Query pointQuery = new Query();
            pointQuery.setFilter(pointFilter);
            SimpleFeatureCollection pointSFC = pointSFS.getFeatures(pointFilter);
            System.out.println("点图层总记录数："+ pointSFC.size());
            FeatureIterator<SimpleFeature> pointFI = pointSFC.features();
            String wtdh = "";//物探点号

            String annoPre = "";
            if(fileType.equals("雨水")){
                annoPre = "YSA";
            }else{
                annoPre = "WSA";
            }
            while (pointFI.hasNext()) {
                SimpleFeature simpleFeature = pointFI.next();
                String elevaton = simpleFeature.getAttribute("Elevation").toString();
                String refName = simpleFeature.getAttribute("RefName").toString();
                Geometry geometry = (Geometry)simpleFeature.getDefaultGeometry();
                Geometry bufGeo = geometry.buffer(Double.valueOf(buffer));
                wtdh = "";
                for(int i = 0 ; i < annoList.size(); i++){
                    SimpleFeature anoSimpleFea = annoList.get(i);
                    boolean isContain = bufGeo.contains((Geometry)anoSimpleFea.getDefaultGeometry());
                    if(isContain){
                        String _refName = anoSimpleFea.getAttribute("RefName").toString();
                        if(_refName.contains(annoPre)){
                            annoList.remove(anoSimpleFea);
                            wtdh = _refName;
                        }
                        break;
                    }
                }
                Point point = (Point) simpleFeature.getDefaultGeometry();
                point.setUserData(wtdh+":"+elevaton);
                //simpleFeature.setAttribute("wtdh",wtdh);
                pointList.add(point);
                Map<Integer,String> mapParam = new HashMap<>();
                mapParam.put(1,wtdh);
                mapParam.put(2,String.valueOf(geometry.getCoordinate().getX()));
                mapParam.put(3,String.valueOf(geometry.getCoordinate().getY()));
                mapParam.put(4,elevaton);
                mapParam.put(7,refName);
                yushuiVoList.add(mapParam);
                System.out.println("物探点号：" + wtdh + ",名称：" + refName + ",高程：" + elevaton + ",坐标：X=" + geometry.getCoordinate().getX() + ":Y=" + geometry.getCoordinate().getY());
            }
            pointFI.close();
            pointDS.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return yushuiVoList;
    }

    //读取shp文件 点
    public static List<Map<Integer,String>> readShpLine(String lineBuffer,String fileType,List<Point> pointList, String lineFilePath, String annoFilePath) {
        List<Map<Integer,String>> yushuiVoList = new ArrayList<>();
        try {
            ShapefileDataStore annoDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(annoFilePath).toURI().toURL());
            annoDS.setCharset( Charset.forName("UTF-8"));
            SimpleFeatureSource annoSFS = annoDS.getFeatureSource(annoDS.getTypeNames()[0]);
            String annoFilterStr = "";
            if(fileType.equals("雨水")){
                annoFilterStr = "Layer='YST' AND RefName like 'YS %'";
            }else{
                annoFilterStr = "Layer='WST' AND RefName like 'WS %'";
            }
            Filter annoFilter = CQL.toFilter(annoFilterStr);
            Query annoQuery = new Query();
            annoQuery.setFilter(annoFilter);
            SimpleFeatureCollection annoSFC = annoSFS.getFeatures(annoFilter);
            Object[] annoObjects = annoSFC.toArray();
            System.out.println("标注图层总记录数："+ annoSFC.size());
            FeatureIterator<SimpleFeature> annoFI = annoSFC.features();
            List<SimpleFeature> annoList = new ArrayList<>();
            while (annoFI.hasNext()) {
                SimpleFeature simpleFeature = annoFI.next();
                annoList.add(simpleFeature);
            }
            annoFI.close();
            annoDS.dispose();
            //源shape文件
            ShapefileDataStore lineDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(lineFilePath).toURI().toURL());
            lineDS.setCharset(  Charset.forName("UTF-8"));
            SimpleFeatureSource lineSFS = lineDS.getFeatureSource(lineDS.getTypeNames()[0]);
            String lineFilterStr = "";
            if(fileType.equals("雨水")){
                lineFilterStr = "Layer='YSL'";
            }else{
                lineFilterStr = "Layer='WSL'";
            }
            Filter lineFilter = CQL.toFilter(lineFilterStr);
            Query lineQuery = new Query();
            lineQuery.setFilter(lineFilter);
            SimpleFeatureCollection lineSFC = lineSFS.getFeatures(lineFilter);
            System.out.println("线图层总记录数："+ lineSFC.size());
            FeatureIterator<SimpleFeature> lineFI = lineSFC.features();
            String wtdh = "";//物探点号
            while (lineFI.hasNext()) {
                SimpleFeature simpleFeature = lineFI.next();
                Geometry multigeometry = (Geometry) simpleFeature.getDefaultGeometry();
                System.out.println(multigeometry.getGeometryType());
                LineString lineString = null;
                double guanchang=0.0;
                if("MultiLineString".equals(multigeometry.getGeometryType())){
                    MultiLineString multiLineString = (MultiLineString)multigeometry;
                    System.out.println(multiLineString.toString());
                    System.out.println(multiLineString.getNumGeometries());
                    lineString = (LineString)multiLineString.getGeometryN(0);
                    guanchang = lineString.getLength();
                }else if("LineString".equals(multigeometry.getGeometryType())){
                    System.out.println(lineString.toString());
                    lineString = (LineString)multigeometry;
                    guanchang = lineString.getLength();
                }else{
                    System.out.println(multigeometry.toString());
                    break;
                }
                //LineString lineString = (LineString)geometry;
                boolean isFindStart = false;
                boolean isFindEnd = false;
                String startWtdh = "";
                String endWtdh = "";
                String startElevation = "";
                String endElevation = "";
                for(int i = 0 ; i < pointList.size(); i++){
                    Point point = pointList.get(i);
                    if(lineString.getStartPoint().equalsExact(point)){
                        startWtdh = point.getUserData().toString().split(":")[0];
                        startElevation = point.getUserData().toString().split(":")[1];
                        isFindStart = true;
                    }else if(lineString.getEndPoint().equalsExact(point)){
                        endWtdh = point.getUserData().toString().split(":")[0];
                        endElevation = point.getUserData().toString().split(":")[1];
                        isFindEnd = true;
                    }
                    if(isFindStart&&isFindEnd) break;
                }
                Geometry bufGeo = multigeometry.buffer(Double.valueOf(lineBuffer));
                wtdh = "";
                String caizhi ="";
                String dmcc = "";
                for(int i = 0 ; i < annoList.size(); i++){
                    SimpleFeature anoSimpleFea = annoList.get(i);
                    boolean isContain = bufGeo.contains((Geometry)anoSimpleFea.getDefaultGeometry());
                    if(isContain){
                        String _refName = anoSimpleFea.getAttribute("RefName").toString();
                        String[] nameArr = _refName.split(" ");
                        caizhi = nameArr[1];
                        dmcc = nameArr[2].replace("Φ","");
                        break;
                    }
                }

                Map<Integer,String> mapParam = new HashMap<>();
                mapParam.put(1,startWtdh);
                mapParam.put(2,endWtdh);
                mapParam.put(6,startElevation);
                mapParam.put(7,endElevation);
                mapParam.put(12,dmcc);
                mapParam.put(14,caizhi);
                mapParam.put(15, String.format("%.3f", guanchang));
                yushuiVoList.add(mapParam);
                System.out.println("起点点号：" + startWtdh + ",终点点号：" + endWtdh + ",起点高程：" + startElevation + ",终点高程：" + endElevation);
            }
            lineFI.close();
            lineDS.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return yushuiVoList;
    }
    public static String writeShp(String filepath) {
        try {
            //创建shape文件对象
            File file = new File(filepath);
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            params.put( ShapefileDataStoreFactory.URLP.key, file.toURI().toURL() );
            ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
            //定义图形信息和属性信息
            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
            tb.setCRS(DefaultGeographicCRS.WGS84);
            tb.setName("shapefile");
            tb.add("the_geom", Point.class);
            tb.add("POIID", Long.class);
            tb.add("NAMEC", String.class);
            ds.createSchema(tb.buildFeatureType());
            ds.setCharset(Charset.forName("GBK"));
            //设置Writer
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
            //写下一条
            SimpleFeature feature = writer.next();
            feature.setAttribute("the_geom", new GeometryFactory().createPoint(new Coordinate(116.123, 39.345)));
            feature.setAttribute("POIID", 1234567890l);
            feature.setAttribute("NAMEC", "某兴趣点1");
            feature = writer.next();
            feature.setAttribute("the_geom", new GeometryFactory().createPoint(new Coordinate(116.456, 39.678)));
            feature.setAttribute("POIID", 1234567891l);
            feature.setAttribute("NAMEC", "某兴趣点2");
            writer.write();
            writer.close();
            ds.dispose();

            //读取刚写完shape文件的图形信息
            ShpFiles shpFiles = new ShpFiles(filepath);
            ShapefileReader reader = new ShapefileReader(shpFiles, false, true, new GeometryFactory(), false);
            try {
                while (reader.hasNext()) {
                    System.out.println(reader.nextRecord().shape());
                }
            } finally {
                reader.close();
            }
        } catch (Exception e) { }
        return  "";
    }

    /**
     * 转换shp文件
     * @param srcfilepath
     * @param destfilepath
     */
    public static void transPointShape(String srcfilepath, String destfilepath) {
        try {
            //源shape文件
            ShapefileDataStore shapeDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(srcfilepath).toURI().toURL());
            //创建目标shape文件对象
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
            params.put(ShapefileDataStoreFactory.URLP.key, new File(destfilepath).toURI().toURL());
            ShapefileDataStore ds = (ShapefileDataStore) factory.createNewDataStore(params);
            // 设置属性
            SimpleFeatureSource fs = shapeDS.getFeatureSource(shapeDS.getTypeNames()[0]);
            //下面这行还有其他写法，根据源shape文件的simpleFeatureType可以不用retype，而直接用fs.getSchema设置
            ds.createSchema(SimpleFeatureTypeBuilder.retype(fs.getSchema(), DefaultGeographicCRS.WGS84));
            SimpleFeatureType schema = fs.getSchema();
            String typeName = schema.getTypeName();
            System.out.println(fs.getSchema());
            //设置writer
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
            //写记录
            SimpleFeatureIterator it = fs.getFeatures().features();
            try {
                int k = 0;
                while (it.hasNext()) {
                    SimpleFeature f = it.next();
                    String polygonStr = f.getDefaultGeometry().toString();
                    //POINT (118.87461466000002 32.094752010000036)
                    String result = "POINT (";
                    String polygonString = polygonStr.replace("POINT (","");
                    polygonString = polygonString.replace(")","");
                    String[] point = polygonString.split(" ");
                    if(point.length == 2){
                        //double lnglat[] = CoordinateTransformUtil.wgs84togcj02(Double.parseDouble(point[0]),Double.parseDouble(point[1]));
                        double lnglat[] = CoordinateTransformUtil.gcj02towgs84(Double.parseDouble(point[0]),Double.parseDouble(point[1]));
                        result = result + lnglat[0] + " " + lnglat[1];
                    }
                    result = result + ")";
                    f.setDefaultGeometry(result);
                    BoundingBox bx =f.getBounds();
                    SimpleFeature fNew = writer.next();
                    fNew.setAttributes(f.getAttributes());
                    writer.write();
                }
            } finally {
                it.close();
            }
            writer.close();
            ds.dispose();
            shapeDS.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 转换面shp文件
     * @param srcfilepath
     * @param destfilepath
     */
    public static void transPolygonShape(String srcfilepath, String destfilepath) {
        try {
            //源shape文件
            ShapefileDataStore shapeDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(srcfilepath).toURI().toURL());
            //创建目标shape文件对象
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
            params.put(ShapefileDataStoreFactory.URLP.key, new File(destfilepath).toURI().toURL());
            ShapefileDataStore ds = (ShapefileDataStore) factory.createNewDataStore(params);
            // 设置属性
            SimpleFeatureSource fs = shapeDS.getFeatureSource(shapeDS.getTypeNames()[0]);
            //下面这行还有其他写法，根据源shape文件的simpleFeatureType可以不用retype，而直接用fs.getSchema设置
            ds.createSchema(SimpleFeatureTypeBuilder.retype(fs.getSchema(), DefaultGeographicCRS.WGS84));
            //设置writer
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
            //写记录
            SimpleFeatureIterator it = fs.getFeatures().features();
            try {
                int k = 0;
                while (it.hasNext()) {
                    SimpleFeature f = it.next();
                    //MULTIPOLYGON (((126.541782 45.727858, 126.542001 45.727989, 126.542205 45.727766, 126.542118 45.727704, 126.542202 45.727595, 126.5421 45.727533, 126.541782 45.727858)))
                    //System.out.println(f.getDefaultGeometry().toString());
                    String polygonString = f.getDefaultGeometry().toString();
                    String result = "MULTIPOLYGON (((";
                    if(polygonString.contains("MULTIPOLYGON") && polygonString.contains("), (")){
                        polygonString = polygonString.replace("MULTIPOLYGON (((","");
                        polygonString = polygonString.replace("))","");
                        String [] lineArr = polygonString.split("\\), \\(");
                        for(int j = 0; j < lineArr.length; j++){
                            String[] points = lineArr[j].split(",");
                            for(int i = 0 ; i< points.length; i++){
                                String srclnglat[] = points[i].trim().split(" ");
                                double lnglat[] = CoordinateTransformUtil.bd09towgs84(Double.parseDouble(srclnglat[0]),Double.parseDouble(srclnglat[1]));
                                result =  result + lnglat[0] + " " + lnglat[1] + ",";
                            }
                            result = result.substring(0,result.length()-1);
                            result = result + "), (";
                        }
                        result = result.substring(0,result.length()-4);
                        result = result + ")))";
                    }else if(polygonString.contains("MULTIPOLYGON")){
                        polygonString = polygonString.substring(polygonString.indexOf("(((") + 3,polygonString.length() - 3);
                        String[] points = polygonString.split(",");
                        for(int i = 0 ; i< points.length; i++){
                            String srclnglat[] = points[i].trim().split(" ");
                            double lnglat[] = CoordinateTransformUtil.bd09towgs84(Double.parseDouble(srclnglat[0]),Double.parseDouble(srclnglat[1]));
                            result =  result + lnglat[0] + " " + lnglat[1] + ",";
                        }
                        result = result.substring(0,result.length()-1);
                        result = result + ")))";
                    }
                    /*String result = "MULTIPOLYGON (((";
                    if(polygonString.contains("MULTIPOLYGON") && polygonString.contains("),(")){
                        System.out.println("存在多面,Id:"+f.getID());
                    }else if(polygonString.contains("MULTIPOLYGON")){
                        polygonString = polygonString.substring(polygonString.indexOf("(((") + 3,polygonString.length() - 3);
                        String[] points = polygonString.split(",");
                        for(int i = 0 ; i< points.length; i++){
                            String srclnglat[] = points[i].trim().split(" ");
                            double lnglat[] = CoordinateTransformUtil.gcj02towgs84(Double.parseDouble(srclnglat[0]),Double.parseDouble(srclnglat[1]));
                            result =  result + lnglat[0] + " " + lnglat[1] + ",";
                        }
                        result = result.substring(0,result.length()-1);
                        result = result + ")))";
                    }*/
                    f.setDefaultGeometry(result);

                    BoundingBox bx =f.getBounds();
                    double lnglatMax[]  = CoordinateTransformUtil.bd09towgs84(bx.getMaxX(),bx.getMaxY());
                    double lnglatMin[]  = CoordinateTransformUtil.bd09towgs84(bx.getMinX(),bx.getMinY());


                    System.out.println(k);
                    SimpleFeature fNew = writer.next();
                    fNew.setAttributes(f.getAttributes());
                    writer.write();
                }
            } finally {
                it.close();
            }
            it.close();
            writer.close();
            ds.dispose();
            shapeDS.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 转换线shp文件
     * @param srcfilepath
     * @param destfilepath
     */
    public static void transLineShape(String srcfilepath, String destfilepath) {
        try {
            //源shape文件
            ShapefileDataStore shapeDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(srcfilepath).toURI().toURL());
            //创建目标shape文件对象
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
            params.put(ShapefileDataStoreFactory.URLP.key, new File(destfilepath).toURI().toURL());
            ShapefileDataStore ds = (ShapefileDataStore) factory.createNewDataStore(params);
            // 设置属性
            SimpleFeatureSource fs = shapeDS.getFeatureSource(shapeDS.getTypeNames()[0]);
            //下面这行还有其他写法，根据源shape文件的simpleFeatureType可以不用retype，而直接用fs.getSchema设置
            ds.createSchema(SimpleFeatureTypeBuilder.retype(fs.getSchema(), DefaultGeographicCRS.WGS84));
            //设置writer
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
            //写记录
            SimpleFeatureIterator it = fs.getFeatures().features();
            try {
                int k = 0;
                while (it.hasNext()) {
                    SimpleFeature f = it.next();
                    //MULTIPOLYGON (((126.541782 45.727858, 126.542001 45.727989, 126.542205 45.727766, 126.542118 45.727704, 126.542202 45.727595, 126.5421 45.727533, 126.541782 45.727858)))
                    //System.out.println(f.getDefaultGeometry().toString());
                    String polygonString = f.getDefaultGeometry().toString();
                    String result = "MULTILINESTRING ((";
                    if(polygonString.contains("MULTILINESTRING") && polygonString.contains("), (")){
                        polygonString = polygonString.replace("MULTILINESTRING ((","");
                        polygonString = polygonString.replace("))","");
                        String [] lineArr = polygonString.split("\\), \\(");
                        for(int j = 0; j < lineArr.length; j++){
                            String[] points = lineArr[j].split(",");
                            for(int i = 0 ; i< points.length; i++){
                                String srclnglat[] = points[i].trim().split(" ");
                                double lnglat[] = CoordinateTransformUtil.wgs84togcj02(Double.parseDouble(srclnglat[0]),Double.parseDouble(srclnglat[1]));
                                result =  result + lnglat[0] + " " + lnglat[1] + ",";
                            }
                            result = result.substring(0,result.length()-1);
                            result = result + "), (";
                        }
                        result = result.substring(0,result.length()-4);
                        result = result + "))";
                    }else if(polygonString.contains("MULTILINESTRING")){
                        polygonString = polygonString.substring(polygonString.indexOf("((") + 2,polygonString.length() - 2);
                        String[] points = polygonString.split(",");
                        for(int i = 0 ; i< points.length; i++){
                            String srclnglat[] = points[i].trim().split(" ");
                            double lnglat[] = CoordinateTransformUtil.wgs84togcj02(Double.parseDouble(srclnglat[0]),Double.parseDouble(srclnglat[1]));
                            result =  result + lnglat[0] + " " + lnglat[1] + ",";
                        }
                        result = result.substring(0,result.length()-1);
                        result = result + "))";
                    }
                    /*String result = "MULTIPOLYGON (((";
                    if(polygonString.contains("MULTIPOLYGON") && polygonString.contains("),(")){
                        System.out.println("存在多面,Id:"+f.getID());
                    }else if(polygonString.contains("MULTIPOLYGON")){
                        polygonString = polygonString.substring(polygonString.indexOf("(((") + 3,polygonString.length() - 3);
                        String[] points = polygonString.split(",");
                        for(int i = 0 ; i< points.length; i++){
                            String srclnglat[] = points[i].trim().split(" ");
                            double lnglat[] = CoordinateTransformUtil.gcj02towgs84(Double.parseDouble(srclnglat[0]),Double.parseDouble(srclnglat[1]));
                            result =  result + lnglat[0] + " " + lnglat[1] + ",";
                        }
                        result = result.substring(0,result.length()-1);
                        result = result + ")))";
                    }*/
                    f.setDefaultGeometry(result);

                    BoundingBox bx =f.getBounds();
                    double lnglatMax[]  = CoordinateTransformUtil.gcj02towgs84(bx.getMaxX(),bx.getMaxY());
                    double lnglatMin[]  = CoordinateTransformUtil.gcj02towgs84(bx.getMinX(),bx.getMinY());


                    System.out.println(k);
                    SimpleFeature fNew = writer.next();
                    fNew.setAttributes(f.getAttributes());
                    writer.write();
                }
            } finally {
                it.close();
            }
            it.close();
            writer.close();
            ds.dispose();
            shapeDS.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //读取shp文件 点
    public static String readSHPJSONO(String srcfilepath){
        String polygonString ="";
        try {
            //源shape文件
            ShapefileDataStore shapeDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(srcfilepath).toURI().toURL());
            // 设置属性
            SimpleFeatureSource fs = shapeDS.getFeatureSource(shapeDS.getTypeNames()[0]);
            //写记录
            SimpleFeatureIterator it = fs.getFeatures().features();
            try
            {
                while (it.hasNext())
                {
                    SimpleFeature f = it.next();
                    polygonString = f.getDefaultGeometry().toString();
                    System.out.println(polygonString);
                    break;
                }
            }finally {
                it.close();
            }
            shapeDS.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return polygonString;
    }
    /**
     * 读取解析zip压缩文件
     * @throws IOException
     */
    public static void readZipContext(String zipPath) throws IOException
    {
        //源shape文件
        ZipFile zf = new ZipFile(zipPath);
        InputStream in = new BufferedInputStream(new FileInputStream(zipPath));
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry ze;
        while((ze = zin.getNextEntry())!=null){
            if(ze.isDirectory()){
                //为空的文件夹什么都不做
            }else{
                System.err.println("file:"+ze.getName()+"\nsize:"+ze.getSize()+"bytes");
                if(ze.getSize()>0){
                    BufferedReader reader;
                    try {
                        reader = new BufferedReader(new InputStreamReader(zf.getInputStream(ze), "utf-8"));
                        String line = null;
                        while((line=reader.readLine())!=null){
                            System.out.println(line);
                        }
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
